﻿using LD.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace LD.Host
{
    public partial class FrmPartNo : BaseFrm
    {
        public string partPath = Path.Combine(Appbus.BasePath, $"Config\\partno.config");
        private readonly string id = Guid.NewGuid().ToString();
        public FrmPartNo()
        {
            InitializeComponent();
            this.Shown += FrmCommunication_Shown;
            this.FormClosing += FrmPartNo_FormClosing;
        }

        private void FrmPartNo_FormClosing(object sender, FormClosingEventArgs e)
        {
            PubSub.Hub.Default.Unsubscribe<LanguageType>(id);
        }

        private void FrmCommunication_Shown(object sender, EventArgs e)
        {
            //权限检查
            base.Permission(UserType.操作工);

            Init();

            ChangeLanguage(LanguageType.中文);
            PubSub.Hub.Default.Subscribe<LanguageType>(id, (p) => ChangeLanguage(p));
        }

        private void Init()
        {
            this.dgv_data.Rows.Clear();
            //var partNoList = Appbus.PartNo.GetList;
            var partNoList = JsonSerializeHelper.Load<List<TB_PartNo>>(partPath);
            if (partNoList == null) { return; }

            for (int i = 0; i < partNoList.Count; i++)
            {
                this.dgv_data.Rows.Add(partNoList[i].Id, partNoList[i].Name);
                this.dgv_data.Rows[i].Selected = false;

                if (partNoList[i].Name.Equals(Appbus.PartNo.CurrentPartNo))
                {
                    this.dgv_data.Rows[i].Selected = true;
                }
            }
        }
        public void New()
        {
            FrmPartNoEdit edit = new FrmPartNoEdit();
            if (edit.ShowDialog() == DialogResult.OK)
            {
                if (Appbus.PartNo.GetList == null)
                    Appbus.PartNo.GetList = new List<TB_PartNo>();

                Appbus.PartNo.GetList.Clear();
                this.dgv_data.Rows.Add(edit.PartNo.Id, edit.PartNo.Name);

                foreach (DataGridViewRow item in dgv_data.Rows)
                {
                    Appbus.PartNo.GetList.Add(new TB_PartNo()
                    {
                        Id = Convert.ToInt32(item.Cells["ID"].Value),
                        Name = item.Cells["配方名称"].Value.ToString()
                    });
                }
                Appbus.PartNo.SaveList();
                Init();
            }
        }

        private void 激活ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var select = this.dgv_data.CurrentRow;
            if (select != null)
            {
                Appbus.PartNo.CurrentPartNo = select.Cells["配方名称"].Value.ToString();

                LoadVision();
            }
        }
        private void LoadVision()
        {
            if ((bool)Cache.GetCache(CONST.VISION)
                && (bool)Cache.GetCache(CONST.VISION_VM))
            {
            }
        }

        private void dgv_data_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            //{
            //    var id = Convert.ToInt32(this.dgv_data[0, e.RowIndex].Value);
            //    var name = this.dgv_data[1, e.RowIndex].Value.ToString();

            //    //弹窗
            //    FrmPartNoEdit edit = new FrmPartNoEdit();
            //    edit.PartNo = new TB_PartNo()
            //    {
            //        Id = id,
            //        Name = name,
            //    };
            //    if (edit.ShowDialog() == DialogResult.OK)
            //    {
            //        this.dgv_data.Rows.Add(edit.PartNo.Id, edit.PartNo.Name);
            //    }

            //    Appbus.PartNo.SaveList();

            //}
        }

        private void 导出流程ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        public void Output()
        {
            var select = this.dgv_data.CurrentRow;
            if (select != null)
            {
                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    sfd.Filter = "Config|*.config";
                    sfd.RestoreDirectory = true;
                    sfd.FileName = select.Cells["配方名称"].Value.ToString();
                    sfd.CheckPathExists = true;
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        string fileName = sfd.FileName;
                        JsonSerializeHelper.Save(Appbus.PartNo.Config.Flow, fileName);
                    }
                }
            }
        }
        public void Input()
        {
            if (MessageBox.Show($"你确定要为【{Appbus.PartNo.CurrentPartNo}】导入新流程吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                using (var sfd = new OpenFileDialog())
                {
                    sfd.Filter = "Config|*.config";
                    sfd.RestoreDirectory = true;
                    sfd.CheckPathExists = true;
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        string fileName = sfd.FileName;
                        Appbus.PartNo.Config.Flow = JsonSerializeHelper.Load<List<TB_Flow>>(fileName);
                        MessageBox.Show("导入后需刷新,保存方可生效");
                    }
                }
            }
        }
        private void 导入流程ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        private void ChangeLanguage(LanguageType type)
        {
            this.btn_copyPartNo.Text = Language.GetString("Copy Recipe");
            this.lab_sourcePartNo.Text = Language.GetString("Source Recipe");
            this.lab_targetPartNo.Text = Language.GetString("Target Recipe");
            this.Text = Language.GetString("Recipe Setting");
        }
        private void btn_copyPartNo_Click(object sender, EventArgs e)
        {
            Appbus.PartNo.CopyPartNo(this.txt_sourcePartNo.Text.Trim(), this.txt_targetPartNo.Text.Trim());
        }
    }
}
